package com.dnjn.happiness.survey.controller.admin;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import com.dnjn.happiness.survey.controller.common.CommonCheckController;
import com.dnjn.happiness.survey.domain.Check;
import com.dnjn.happiness.survey.domain.vo.CheckItemVO;
import com.dnjn.happiness.survey.domain.vo.CheckVO;
import com.dnjn.happiness.common.log.annotation.Log;
import com.dnjn.happiness.common.log.enums.BusinessType;
import com.dnjn.happiness.common.core.utils.bean.BeanUtils;
import com.dnjn.happiness.common.core.utils.poi.ExcelUtil;
import com.dnjn.happiness.system.api.domain.SysTeam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.dnjn.happiness.common.security.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 考核记录Controller
 *
 * @author happiness
 * @date 2024-04-02
 */
@Tag(name = "check  admin check 考核记录", description = "考核记录")
@RestController
@RequestMapping("/admin/check/check")
public class AdminCheckController extends CommonCheckController {

    /**
     * 导出考核记录列表
     */
    @Log(title = "考核记录", businessType = BusinessType.EXPORT)
    @RequiresPermissions("@ss.hasPermi('check :check:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, CheckVO vo) {
        Check check = new Check();
        BeanUtils.copyPropsIgnoreNull(vo, check);

        List<Check> list = checkService.list(Wrappers.query(check));
        ExcelUtil<Check> util = new ExcelUtil<Check>(Check.class);
        util.exportExcel(response, list, "考核记录数据");
    }


    /*
    查询所有普通员工
     */
    @Log(title = "考核记录", businessType = BusinessType.SELECT)
    @RequiresPermissions("@ss.hasAnyPermi('check :check:query,ignore:check:query')")
    @PostMapping(value = "/getCheckEmployees")
    @Operation(summary = "getCheckEmployees")
    public AjaxResult getCheckEmployees(@RequestBody CheckVO vo) {

        List<SysTeam> sysTeams = checkService.getCheckList(vo);
        return AjaxResult.success(sysTeams);
    }


    @Log(title = "考核记录", businessType = BusinessType.SELECT)
    @RequiresPermissions("@ss.hasAnyPermi('check :check:query,ignore:check:query')")
    @PostMapping(value = "/getManager")
    @Operation(summary = "/getManager查询管理员")
    public AjaxResult getManager(@RequestBody CheckVO vo) {

        List<SysTeam> sysTeams = checkService.getCheck(vo);
        return AjaxResult.success(sysTeams);
    }


    @Log(title = "考核记录", businessType = BusinessType.SELECT)
    @RequiresPermissions("@ss.hasAnyPermi('check :check:query,ignore:check:query')")
    @GetMapping(value = "/getByManagerId")
    @Operation(summary = "getByManagerId 查询上边两级")
    public AjaxResult getByManagerId(Long userId) {

        List<SysTeam> sysTeams = checkService.getByManagerId(userId);
        return AjaxResult.success(sysTeams);
    }

    // Subordinate

    @Log(title = "考核记录", businessType = BusinessType.SELECT)
    @RequiresPermissions("@ss.hasAnyPermi('check :check:query,ignore:check:query')")
    @GetMapping(value = "/getSubordinate")
    @Operation(summary = "getSubordinate 查询下级")
    public AjaxResult getSubordinate(Long userId) {

        List<SysTeam> sysTeams = checkService.getSubordinate(userId);
        return AjaxResult.success(sysTeams);
    }


    @Log(title = "考核记录", businessType = BusinessType.LIST)
    @RequiresPermissions("@ss.hasAnyPermi('check :check:list,ignore:check:list')")
    @GetMapping("/queryList")
    public AjaxResult queryList(@RequestParam(required = false) String checkType, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Long deptId = SecurityUtils.getLoginUser().getDeptId();

        Page page = Page.of(current, size);
        Page<Check> list = checkService.queryList(page, checkType,deptId);
        return AjaxResult.success(list);
    }

    @Log(title = "考核记录", businessType = BusinessType.SELECT)
    @RequiresPermissions("@ss.hasAnyPermi('check :check:query,ignore:check:query')")
    @GetMapping("/queryCheckItemByCheckId")
    @Operation(summary = "queryCheckItemByCheckId绩效考核,item详情页,根据考核人姓名,考核状态(Y|N)搜索,新增团队,部门搜索")
    public AjaxResult queryCheckItemByCheckId(String checkId, @RequestParam(required = false) String nickName, @RequestParam(required = false) String checkStatus, @RequestParam(required = false) Long deptId, @RequestParam(required = false) Long teamId, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {

        Page page = Page.of(current, size);
        Page<CheckItemVO> checkItemPage = checkService.queryCheckItemByCheckId(page, nickName, checkStatus, checkId, deptId, teamId);
        return AjaxResult.success(checkItemPage);
    }
}
